package com.lxy.cxcyxmmsbackend.sys.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.lxy.cxcyxmmsbackend.sys.entity.CxcyXm;
import com.lxy.cxcyxmmsbackend.sys.entity.KeyValue;
import com.lxy.cxcyxmmsbackend.sys.entity.XmHjqk;
import com.lxy.cxcyxmmsbackend.sys.req.PageReq;
import com.lxy.cxcyxmmsbackend.sys.res.Result;
import com.lxy.cxcyxmmsbackend.sys.service.CxcyXmService;
import com.lxy.cxcyxmmsbackend.sys.service.KeyValueService;
import com.lxy.cxcyxmmsbackend.sys.service.XmHjqkService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.models.auth.In;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.time.Year;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * @author lxy
 * @title 创新创业项目统计
 * @date 2024/4/12
 **/
@Api(tags = "创新创业项目统计")
@RestController
@RequestMapping("/cxcyXmtj")
public class CxcyxmTjController {

    @Resource
    CxcyXmService cxcyXmService;

    @Autowired
    KeyValueService keyValueService;

    @Autowired
    XmHjqkService xmHjqkService;

    @ApiOperation("查询各学院项目在驻比例")
    @RequestMapping("queryCxcyXmZZBL")
    public Result queryCxcyXmZZBL() {
        ArrayList<Map<String, Object>> data = new ArrayList<>();



        //查询学院字典
        QueryWrapper<KeyValue> wrapper = new QueryWrapper<>();
        wrapper.lambda().eq(KeyValue::getKeycode,"xy");

        QueryWrapper<CxcyXm> cxcyXmQueryWrapper = new QueryWrapper<>();
        List<KeyValue> list = keyValueService.list(wrapper);
        for (KeyValue keyValue : list) {
            Map<String, Object> dataItem = new HashMap<>();
            dataItem.put("name",keyValue.getKeyname());
            cxcyXmQueryWrapper = new QueryWrapper<>();
            cxcyXmQueryWrapper.lambda().like(CxcyXm::getXmSsxy,keyValue.getKeyname());
            cxcyXmQueryWrapper.lambda().eq(CxcyXm::getXmzt,'5');
           int count = cxcyXmService.list(cxcyXmQueryWrapper).size();
            dataItem.put("value",count);

            data.add(dataItem);
        }
        return Result.success("获取各学院项目在驻比例成功",data);
    }

    @ApiOperation("查询各类型项目比例")
    @RequestMapping("queryCxcyXmLXBL")
    public Result queryCxcyXmLXBL() {
        ArrayList<Map<String, Object>> data = new ArrayList<>();

        //查询学院字典
        QueryWrapper<KeyValue> wrapper = new QueryWrapper<>();
        wrapper.lambda().eq(KeyValue::getKeycode,"xmlx");

        QueryWrapper<CxcyXm> cxcyXmQueryWrapper = new QueryWrapper<>();
        List<KeyValue> list = keyValueService.list(wrapper);
        for (KeyValue keyValue : list) {
            Map<String, Object> dataItem = new HashMap<>();
            dataItem.put("name",keyValue.getKeyname());
            cxcyXmQueryWrapper = new QueryWrapper<>();
            cxcyXmQueryWrapper.lambda().like(CxcyXm::getXmXmlxbh,keyValue.getKeyvalue());
            cxcyXmQueryWrapper.lambda().eq(CxcyXm::getXmzt,'5');
            int count = cxcyXmService.list(cxcyXmQueryWrapper).size();
            dataItem.put("value",count);

            data.add(dataItem);
        }
        return Result.success("获取各类型项目比例成功",data);
    }


    @ApiOperation("近五年各学院获奖趋势")
    @RequestMapping("queryCxcyXmHJFiveYear")
    public Result queryCxcyXmHJFiveYear() {
        HashMap<String, Object> resultMap = new HashMap<>();
        ArrayList<Map<String, Object>> seriesList = new ArrayList<>();
        ArrayList<String> xyMcList = new ArrayList<>();
        //查询学院字典
        QueryWrapper<KeyValue> wrapper = new QueryWrapper<>();
        wrapper.lambda().eq(KeyValue::getKeycode,"xy");

        int currentYear = Year.now().getValue();
        List<Integer> yearList = new ArrayList<>();

        for (int i = 4; i > -1; i--) {
            yearList.add(currentYear - i);
        }

        QueryWrapper<XmHjqk> xmHjqkQueryWrapper = new QueryWrapper<>();
        List<KeyValue> list = keyValueService.list(wrapper);

        QueryWrapper<CxcyXm> cxcyXmQueryWrapper = new QueryWrapper<>();
        for (KeyValue keyValue : list) {
            xyMcList.add(keyValue.getKeyname());

            Map<String, Object> dataItem = new HashMap<>();
            dataItem.put("type","line");
            dataItem.put("smooth", false);
            //隔空值相连
            dataItem.put("connectNulls", true);
            dataItem.put("name",keyValue.getKeyname());
            ArrayList<Integer> hjCount = new ArrayList<>();
            for (Integer year : yearList) {
                xmHjqkQueryWrapper = new QueryWrapper<>();
                xmHjqkQueryWrapper.lambda().gt(XmHjqk::getHjsj, year+"-01-01")
                        .lt(XmHjqk::getHjsj, year+"-12-31");
                xmHjqkQueryWrapper.select("distinct XM_BH");
                //查询该学院项目
                cxcyXmQueryWrapper = new QueryWrapper<>();
                cxcyXmQueryWrapper.lambda().like(CxcyXm::getXmSsxy,keyValue.getKeyname());
                List<CxcyXm> list1 = cxcyXmService.list(cxcyXmQueryWrapper);
                if (list1!=null && !list1.isEmpty()){
                    List<String> xmBhList = list1.stream().map(CxcyXm::getXmBh).collect(Collectors.toList());

                    xmHjqkQueryWrapper.lambda().in(XmHjqk::getXmBh,xmBhList);
                    int count = xmHjqkService.list(xmHjqkQueryWrapper).size();
                    hjCount.add(count);
                }


            }

            dataItem.put("data",hjCount);

            seriesList.add(dataItem);
        }

        resultMap.put("xyMcList",xyMcList);
        resultMap.put("yearList",yearList);
        resultMap.put("seriesList",seriesList);
        return Result.success("获取各类型项目比例成功",resultMap);
    }


    @ApiOperation("近五年各类型获奖趋势")
    @RequestMapping("queryCxcyXmHJFiveYearByLX")
    public Result queryCxcyXmHJFiveYearByLX() {
        HashMap<String, Object> resultMap = new HashMap<>();
        ArrayList<Map<String, Object>> seriesList = new ArrayList<>();
        ArrayList<String> xyMcList = new ArrayList<>();
        //查询学院字典
        QueryWrapper<KeyValue> wrapper = new QueryWrapper<>();
        wrapper.lambda().eq(KeyValue::getKeycode,"xmlx");

        int currentYear = Year.now().getValue();
        List<Integer> yearList = new ArrayList<>();

        for (int i = 4; i > -1; i--) {
            yearList.add(currentYear - i);
        }

        QueryWrapper<XmHjqk> xmHjqkQueryWrapper = new QueryWrapper<>();
        List<KeyValue> list = keyValueService.list(wrapper);

        //emphasisStyle
        HashMap<String, Object> emphasisStyle = new HashMap<>();
        HashMap<String, Object> itemStyle = new HashMap<>();
        itemStyle.put("shadowBlur",10);
        itemStyle.put("shadowColor","rgba(0,0,0,0.3)");
        emphasisStyle.put("itemStyle",itemStyle);

        QueryWrapper<CxcyXm> cxcyXmQueryWrapper = new QueryWrapper<>();
        for (KeyValue keyValue : list) {
            xyMcList.add(keyValue.getKeyname());

            Map<String, Object> dataItem = new HashMap<>();
            dataItem.put("type","bar");
            dataItem.put("stack", "one");
            //隔空值相连
            dataItem.put("barWidth", "60%");
            dataItem.put("emphasis", emphasisStyle);
            dataItem.put("name",keyValue.getKeyname());

            ArrayList<Integer> hjCount = new ArrayList<>();
            for (Integer year : yearList) {
                xmHjqkQueryWrapper = new QueryWrapper<>();
                xmHjqkQueryWrapper.lambda().gt(XmHjqk::getHjsj, year+"-01-01")
                        .lt(XmHjqk::getHjsj, year+"-12-31");
                xmHjqkQueryWrapper.select("distinct XM_BH");
                //查询该类型项目
                cxcyXmQueryWrapper = new QueryWrapper<>();
                cxcyXmQueryWrapper.lambda().like(CxcyXm::getXmXmlxbh,keyValue.getKeyvalue());
                List<CxcyXm> list1 = cxcyXmService.list(cxcyXmQueryWrapper);
                if (list1!=null && !list1.isEmpty()){
                    List<String> xmBhList = list1.stream().map(CxcyXm::getXmBh).collect(Collectors.toList());

                    xmHjqkQueryWrapper.lambda().in(XmHjqk::getXmBh,xmBhList);
                }

                int count = xmHjqkService.list(xmHjqkQueryWrapper).size();
                hjCount.add(count);
            }

            dataItem.put("data",hjCount);

            seriesList.add(dataItem);
        }

        resultMap.put("lxMcList",xyMcList);
        resultMap.put("yearList",yearList);
        resultMap.put("seriesList",seriesList);
        return Result.success("获取各类型项目比例成功",resultMap);
    }

}
